﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Configuration;
using System.Xml;
using System.Reflection;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Fcdbas.Core.DataAccess;
using Fcdbas.Core.Reflection;

namespace Fcdbas.WinUI.Design
{
    public partial class frmLoginDb : DevExpress.XtraEditors.XtraForm
    {
        public frmLoginDb()
        {
            InitializeComponent();
        }

        private void frmLoginDb_Load(object sender, EventArgs e)
        {



        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
        public string DbType
        {
            get
            {
                return cboDataBaseSelect.EditValue.ToString();
            }
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cboDataBaseSelect.EditValue.ToString()))
            {
                XtraMessageBox.Show("请选择数据库！", "选择数据库");
                return;
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }
        // private Dictionary<string, ConnectionStringSettings> cons = new Dictionary<string, ConnectionStringSettings>();
        private void btnOpen_Click(object sender, EventArgs e)
        {
            string filePath = System.IO.Directory.GetCurrentDirectory() + "App.config";
            if (!System.IO.File.Exists(filePath))
            {
                if (openFileDialog1.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    filePath = openFileDialog1.FileName;
                }
                else
                {
                    return;
                }
            }
            AssemblyX.DevEn = true;
            XmlDocument doc = new XmlDocument();

            try
            {
                doc.Load(filePath);
            }
            catch (XmlException ex)
            {

                Console.WriteLine(ex.Message);
                throw;
            }
            XmlElement root = doc.DocumentElement;


            if (root.ChildNodes != null && root.ChildNodes.Count > 0)
            {
                foreach (XmlNode item in root.ChildNodes)
                {
                    if (item.Name != "connectionStrings")
                        continue;

                    if (item.ChildNodes != null && (item.ChildNodes.Count > 1))
                    {
                        foreach (XmlNode cItem in item.ChildNodes)
                        {
                            if (cItem.Attributes == null)
                                continue;
                            string dbType = cItem.Attributes[0].Value;
                            string connnectstring = cItem.Attributes[1].Value;
                            string provider = cItem.Attributes[2].Value;
                            Type dType = null;
                            //AssemblyX asmx;
                            //var asm2 = Assembly.LoadFile(System.IO.Directory.GetCurrentDirectory() + "\\Fcdbas.Core.dll");
                            //asmx = AssemblyX.Create(asm2);


                            if (dbType.ToUpper() == "MYSQL")
                                dType = typeof(Fcdbas.Core.DataAccess.MySql);
                            else if (dbType.ToUpper() == "ORACLE")
                                dType = typeof(Fcdbas.Core.DataAccess.Oracle);
                            cboDataBaseSelect.Properties.Items.Add(cItem.Attributes[0].Value);

                            DAL.AddConnStr(dbType, connnectstring, dType, provider);
                        }
                    }

                }
            }
        }
    }
}